package com.wly.adapter
{
	import com.wly.adapter.common.AdapterBase;
	import com.wly.ui.ItemGrid;
	
	import flash.display.BitmapData;
	import flash.events.IEventDispatcher;
	
	public class ItemGridAdapter extends AdapterBase
	{
		private var _itemBmds:Array;
		private var _startIndex:int;
		private var _parentAdapter:ToolbarAdapter;
		
		public function ItemGridAdapter()
		{
			super();
			_itemBmds = [];
		}
		
		public function set parentAdapter(parentAdapter:ToolbarAdapter):void
		{
			_parentAdapter = parentAdapter;
		}
		
		public function get effLen():int
		{
			var tmp:int = length - _startIndex;
			if(tmp > ItemGrid.GRIDCAPACITY)
			{
				return ItemGrid.GRIDCAPACITY;
			}
			return tmp;
		}
		
		public function getAt(i:int):BitmapData
		{
			return _itemBmds[_startIndex+i] as BitmapData;
		}
		
		public function get length():int
		{
			return _itemBmds.length;
		}
		
		private function set startIndex(start:int):void
		{
			_startIndex = start;
			refreshNotify();
		}
		
		public function onPreItem():void
		{
			if(_startIndex)
			{
				startIndex = _startIndex-1;
			}
		}
		
		public function onPrePage():void
		{
			if(_startIndex < ItemGrid.GRIDCAPACITY)
			{
				startIndex = 0;
			}
			else
			{
				startIndex = _startIndex-ItemGrid.GRIDCAPACITY;
			}
		}
		
		public function onNextItem():void
		{
			if(_startIndex+ItemGrid.GRIDCAPACITY<length)
			{
				startIndex = _startIndex+1;
			}
		}
		
		public function onNextPage():void
		{
			if(_startIndex+2*ItemGrid.GRIDCAPACITY<length)
			{
				startIndex = _startIndex+ItemGrid.GRIDCAPACITY;
			}
			else
			{
				startIndex = length - ItemGrid.GRIDCAPACITY;
			}
		}
		
		public function onSelect(index:int):void
		{
			var bmd:BitmapData;
			bmd = _itemBmds[_startIndex+index] as BitmapData;
			if(bmd && _parentAdapter)
			{
				_parentAdapter.onItemSelect(bmd);
			}
		}
		
		public function updateData(bmdArr:Array):void
		{
			_itemBmds = bmdArr;
			refreshNotify();
		}
	}
}